
var app = app || {}

/**
 *  開啟工作視窗之後, 將該 window 的連結儲存至此, 以方便操作
 */
app.popupWindowLink = null;

/**
 *  將打開的視窗做保護
 *  讓原呼叫的主視窗 opener.window 失去了子視窗的情況下, 盡量能取回原有的控制權
 *  
 *  @see http://josephj.com/entry.php?id=367
 */
app.openWorkWindow = function()
{
    var width = 860,
        height = 600,
        left = (screen.width-width)/2,
        top = (screen.height-height)/2,
        CHILD_WINDOW_NAME = "work_window", // It's important to have a consistent name.
        CHILD_WINDOW_URL = this.attributes['situs'].value,
        CHILD_WINDOW_ATTRS = "width="+width+",height="+height+",top="+top+",left="+left ,
        win = null;

    // Window object is existing.
    if (win) {
        if (!win.closed) {
            // Child Window exists
        }
        else {
            // Child Window is closed, Open child window again.
            win = window.open(CHILD_WINDOW_URL, CHILD_WINDOW_NAME, CHILD_WINDOW_ATTRS);
        }
    }
    else {
        // Child Window doesn't exist or Parent Window is refreshed
        win = window.open("", CHILD_WINDOW_NAME, CHILD_WINDOW_ATTRS);
        if (win.location.href === "about:blank") {
            // Child window doesn't exist.(Confirmed)
            win = window.open(CHILD_WINDOW_URL, CHILD_WINDOW_NAME, CHILD_WINDOW_ATTRS);
        }
        else {
            // Child window is reconnected again
            // win.document.getElementById("wrap").innerHTML += "Reconnection happened at...<br> " + new Date().toString() + "<br>";
            win.focus();
        }
    }

    app.popupWindowLink = win;
    return win;

}

/**
 *  建立一個統一的 function 名稱
 *  以方便當呼叫的子視窗傳回 messages 的時候統一由該 function 處理後續動作 
 */
app.doResultHook = null;




